package com.wsoft.mq.test.sample.headers;

import com.wsoft.mq.test.constant.RabbitHeadersConstant;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author z001
 * @Date 2025/1/7 15:36
 */
@RestController
@RequestMapping("/headers")
public class HeadersSample {
    @Resource
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/sendMessage1")
    public void sendMessage1(@RequestParam("message") String message) {
        Map<String, Object> headers  = new HashMap<>(2);
        headers.put("type", "report");
        headers.put("format", "pdf");
        rabbitTemplate.convertAndSend(RabbitHeadersConstant.HEADERS_EXCHANGE
                , "",
                message, msg -> {
                    msg.getMessageProperties().getHeaders().putAll(headers);
                    return msg;
                });
    }

    @GetMapping("/sendMessage2")
    public void sendMessage2(@RequestParam("message") String message) {
        Map<String, Object> headers  = new HashMap<>(2);
        headers.put("type", "report");
        headers.put("format", "txt");
        rabbitTemplate.convertAndSend(RabbitHeadersConstant.HEADERS_EXCHANGE
                , "",
                message, msg -> {
                    msg.getMessageProperties().getHeaders().putAll(headers);
                    return msg;
                });
    }
}
